﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NZBMatrixAdvancedFeedReader.Classes
{
    public class CommonTasks
    {
        
        /// <summary>
        /// Displays Error Message Box.
        /// </summary>
        /// <param name="message"></param>
        public static void ShowErrorMesssage(string message)
        {
            MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// Displays Confirmation Dialog Box with Yes/No option.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public static DialogResult ShowQuestionMessage(string message, string title)
        {
            return MessageBox.Show(message, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        }

        /// <summary>
        /// Read the response from the specific URL and returns the string content.
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string ReadURLToString(string url)
        {
            return ReadURLToString(url, -1);
        }

        /// <summary>
        /// Read the response from the specific URL and returns the string content.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="maxLength">Maximum number of bytes to read from host.</param>
        /// <returns></returns>
        public static string ReadURLToString(string url, long maxLength)
        {

            System.Net.WebRequest objRequest;
            System.Net.WebResponse objResponse;
            System.IO.Stream objResponseStream;
            int objResponseByte;
            List<byte> objResponseData;
            string objResponseXML;

            objRequest = System.Net.HttpWebRequest.Create(url);
            ((System.Net.HttpWebRequest)objRequest).UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";

            try
            {
                objResponse = objRequest.GetResponse();



                objResponseStream = objResponse.GetResponseStream();

                objResponseData = new List<byte>();


                do
                {

                    objResponseByte = objResponseStream.ReadByte();

                    if (objResponseByte < 0)
                        break;

                    objResponseData.Add((byte)objResponseByte);

                } while (objResponseData.Count <= maxLength || maxLength == -1);

                objRequest = null;
                objResponse.Close();
                objResponseStream.Close();



                objResponseXML = System.Text.Encoding.GetEncoding(System.Text.Encoding.UTF8.CodePage).GetString(objResponseData.ToArray());

            }
            catch (Exception ex)
            {

                objResponseXML = ex.Message;
            }

            return objResponseXML;

        }

        /// <summary>
        /// Fix Hex strings in the XML data from Hex code to actual characters.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string FixHexStrings(String value)
        {
            
            if (value.Contains("&#x"))
            {
                int index1, index2;
                index1 = 0;

                do
                {

                    index1 = value.IndexOf("&#x",index1);
                    if (index1 > 0)
                    {
                        index1 += 3;
                        index2 = value.IndexOf(";", index1);
                        if (index2 > 0)
                        {
                            string hexString = value.Substring(index1,index2-index1);
                            value = value.Replace("&#x" + hexString + ";" , ConvertHexToString(hexString));
                        }


                    }

                } while (index1>0);
                
                
            }

            return value;
        }
        
        /// <summary>
        /// Converts a Hex string to equivalent string. 
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static string ConvertHexToString(string hexString)
        {
            
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < hexString.Length; i += 2)
            {
                string hs = hexString.Substring(i, 2);
                sb.Append(Convert.ToChar(Convert.ToUInt32(hs, 16)));

            }
            return sb.ToString();

        }

        /// <summary>
        /// Writes logs into the console output.
        /// </summary>
        /// <param name="message"></param>
        public static void WriteLog(string message)
        {
            System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.ffff") + " " + message );
        }

    }
}
